package cn.hdax.test1;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

public class CreateTable {
    // 创建表
    public static void create(String tableName, String... familyNames)
            throws Exception {

        //
        Configuration configuration = HBaseConfiguration.create();

        Connection conn = ConnectionFactory.createConnection(configuration);
        Admin admin = conn.getAdmin();
        // TableName:以POJO对象来封装表的名字
        TableName tn = TableName.valueOf(tableName);
        if (admin.tableExists(tn)) { // 如果存在先删除
            admin.disableTable(tn);// 先使表无效
            admin.deleteTable(tn);
        }
        // HTableDescriptor包含了表的名字及其对应的列族
        HTableDescriptor htd = new HTableDescriptor(tn);
        for (String family : familyNames)
            htd.addFamily(new HColumnDescriptor(family));
        admin.createTable(htd);
        conn.close();
        System.out.println("create success!");
    }

    public static void main(String[] args) throws Exception {
        create("student","info") ;
    }
}
